$(document).ready(function(){
	selectAll();
});

function selectAll() {
	var uid = $("#loginUserId").text();
	$.ajax({
		type: "get",
		url: "ajaxQueryShoppingCart",
		data: "uid="+uid,
		success: function (data) {
			console.log(data);
			var strMsg = "";
			$.each(data, function (index, eachGoods) {
				strMsg += "<tr class='product'>";
				strMsg += "<td class='gid' hidden='hidden'>" + eachGoods.gid + "</td>";
				strMsg += "<td class='cart_td_1'><input class='checksin' name='cartCheckBox' type='checkbox' value='product1' onclick='danxuan()'/></td>";
				strMsg += "<td class='cart_td_2'><img src='" + eachGoods.image + "' alt='shopping'/></td>";
				strMsg += "<td class='cart_td_3'>";
				strMsg += "<a href='#'>" + eachGoods.gname + "</a><br />";
				strMsg += "</td>";
				strMsg += "<td class='cart_td_4'>" + eachGoods.accumulatePoints + "</td>";
				strMsg += "<td class='cart_td_5'>" + eachGoods.price.toFixed(2) + "</td>";
				strMsg += "<td class='cart_td_6'>";
				strMsg += "<img src='img/shoppingCar-minus.jpg' alt='minus' name='minus' class='hand sub' onclick='jianhao(\"" + eachGoods.gid + "\",this)'/>";
				strMsg += " <input type='text' value='" + eachGoods.num + "' class='num_input' readonly='readonly'/> ";
				strMsg += "<img src='img/shoppingCar-adding.jpg' alt='add' name='add' class='hand plus' onclick='jiahao(\"" + eachGoods.gid + "\",this)'/>";
				strMsg += "</td>";
				strMsg += "<td class='cart_td_7'>" + (eachGoods.num * eachGoods.price.toFixed(2)).toFixed(2) + "</td>";
				strMsg += "<td class='cart_td_8'><a name='deleteRow' class='deleteRow' onclick='shanchubenhang(\"" + eachGoods.gid + "\",this)'>删除</a></td>";
				strMsg += "</tr>";
			});
			$("#products").html(strMsg); // 局部刷新
		},
		error: function () {
			alert("出错啦");
		}
	});
}

function danxuan() {
	if($(".checksin:checked").length == $(".checksin").length){
		$("#checkAll").prop("checked","checked");
	} else {
		$("#checkAll").prop("checked", "");
	}
	// 总价计算
	result();
}

function jiahao(gid,which) {
	var uid = $("#loginUserId").text();
	var num = parseInt($(which).prev().val()) + 1;
	$.ajax({
		type: "PUT",
		url: "ajaxUpdateShoppingCart",
		data: "uid="+uid+"&gid="+gid+"&num="+num,
		success: function (data) {
			if (data && data == 'ok') {
				$(which).prev().val(parseInt($(which).prev().val())+1);
				$(which).parent().next().text((parseFloat($(which).parent().next().text()) + parseFloat($(which).parent().prev().text())).toFixed(2));
				result();
			} else {
				alert("出错啦，请刷新后重试");
			}
		},
		error: function () {
			alert("出错啦，请刷新后重试");
		}
	});
}

function jianhao(gid,which) {
	var uid = $("#loginUserId").text();
	var num = parseInt($(which).next().val()) - 1;
	if (num > 0) {
		$.ajax({
			type: "PUT",
			url: "ajaxUpdateShoppingCart",
			data: "uid="+uid+"&gid="+gid+"&num="+num,
			success: function (data) {
				if (data && data == 'ok') {
					$(which).next().val(parseInt($(which).next().val() - 1));
					$(which).parent().next().text((parseFloat($(which).parent().next().text()) - parseFloat($(which).parent().prev().text())).toFixed(2));
					result();
				} else {
					alert("出错啦，请刷新后重试");
				}
			},
			error: function () {
				alert("出错啦，请刷新后重试");
			}
		});
	}
}

function shanchubenhang(gid,which) {
	var uid = $("#loginUserId").text();
	$.ajax({
		type: "DELETE",
		url: "ajaxDeleteShoppingCart",
		data: "uid="+uid+"&gid="+gid,
		success: function (data) {
			if (data && data == 'ok') {
				$(which).parent().parent().remove();
				result();
				alert("删除成功");
			} else {
				alert("出错啦，请刷新后重试");
			}
		},
		error: function () {
			alert("出错啦，请刷新后重试");
		}
	});
}

$(document).ready(function(){
	// 全选反选
	$("#checkAll").on("click", function() {
		$(".checksin").prop("checked", this.checked);
		// 调用总价计算
		result();
	});
});

// 总价计算
function result() {
	var allPrice = 0.0; // 总价
	var allIntegral = 0; // 总积分

	$(".product").each(function(){
		if($(this).find(".checksin")[0].checked == true){
			allPrice += parseFloat($(this).find(".cart_td_7").text());
			allIntegral += parseInt($(this).find(".cart_td_4").text()) * parseInt($(this).find(".num_input").val());
		} else {
			allPrice += 0;
			allIntegral += 0;
		}
	});
	allPrice = allPrice.toFixed(2);
	$("#total").html(allPrice);
	$("#integral").text(allIntegral);
}

// 删除选中
function delSelect() {
	var uid = $("#loginUserId").text();

	$(".product").each(function() {
		if ($(this).find(".checksin")[0].checked == true) {
			$(this).remove();
			$.ajax({
				type: "DELETE",
				url: "ajaxDeleteShoppingCart",
				data: "uid="+uid+"&gid="+$(this).find(".gid").text(),
				success: function (data) {
					if (data && data == 'ok') {
						//
					} else {
						alert("出错啦，请刷新后重试");
					}
				},
				error: function () {
					alert("出错啦，请刷新后重试");
				}
			});
		}
	});
	alert("删除成功");
	// 总价计算
	result();
}
